<?php
namespace Home\Controller;
use Think\Controller;
header('Content-Type:text/html; charset=utf-8');
class IndexController extends Controller {
	
	//初始化
	function __construct(){
        header("Content-type: text/html; charset=utf-8");
		parent::__construct();
		//识别是否调用的USER类模板
		$is_user =  (int)is_numeric(strpos(__ACTION__,"user"));
		$this->assign('is_user', $is_user);
		if(!empty($_SESSION['islogin'])){
			$this->assign('islogin',(int)$_SESSION['islogin']);
		}

        //footer尾部
        $category= M('news_category');
        $news= M('News');
        $new_list= $news->where("ntype=1")->select();
        $HR= $category->where("ntype=5")->order('sort asc')->select();
        $down_list= $category->where("ntype=11")->order('sort asc')->select();
        foreach ($HR as $key => $value){
            if($value['name']=="案例介绍"){
                $this->assign('case_id', $value['id']);
            }

        }

        $this->assign('new_list', $new_list);
        $this->assign('HR', $HR);
        $this->assign('down_list', $down_list);

	}

    //首页
    public function index(){

        $news= D("News");
        $links=D('Link');

        $list= $news->where("ntype=1")->select();
        $link= $links->order('sort asc')->select();

//        foreach ($link as $key=>$value){
//              $imgurl= $link[$key]['imgurl'];
//              $imgrul= explode("/", $imgurl);
//              $link[$key]['imgurl']= './'.$imgrul[3].'/'.$imgrul[4].'/'.$imgrul[5];
//        }

        //政策法规
        $policy= $news->where('ntype=3 and is_show=1')->limit(7)->order('newsdt desc')->select();

        $law= $news->where('ntype=5 and is_show=1')->limit(7)->order('newsdt desc')->select();

        //截取时间字符串
        foreach ($policy as $key => $value){

            $policy[$key]['newsdt']= substr($value['newsdt'], 0, 10);
        }

        foreach ($law as $key => $value){

            $law[$key]['newsdt']= substr($value['newsdt'], 0, 10);
        }

        //首页banner图
        $sliders= D("Sliders");
        $bg_img= $sliders->where("ntype=1 and is_show=1 ")->field('pic,url')->select();

        //职位发布
        $position= D("Positions");
        $posi= $position->where('status=1')->limit(6)->order('update_time desc')->select();

        //热门职位
        $hot= $position->where('status=1 AND hot=1 ')->limit(6)->order('update_time desc')->select();

        foreach ($posi as $key => $value){

            $posi[$key]['update_time']= substr($value['update_time'], 0, 10);
            $posi[$key]['xzfl']= substr($value['xzfl'], 0, 27);

            $posi[$key]['xzfl']=rtrim($posi[$key]['xzfl'], '，');
        }

        foreach ($hot as $key => $value){

            $hot[$key]['update_time']= substr($value['update_time'], 0, 10);
            $hot[$key]['xzfl']= substr($value['xzfl'], 0, 27);
            $hot[$key]['xzfl']=rtrim($posi[$key]['xzfl'], '，');
        }

        $name= __FUNCTION__; $this->assign('name', $name);
        $this->assign('list', $list);
        $this->assign('bg_img', $bg_img);
        $this->assign('policy', $policy);
        $this->assign('posi',  $posi);
        $this->assign('law',  $law);
        $this->assign('hot',  $hot);
        $this->assign('link',  $link);
        $this->display();
    }

    //用户中心左边侧边栏
    public function header(){

        if($_SESSION['islogin']== '1'){

            $users=D('Users');
            $user= $users->where('id=' .session('id'))->find();

            $this->assign('user', $user);


        }else{
            $this->error("请登入!", __APP__ . "/index.php/login");

        }



    }

    //关于我们
    public function about(){

        $news= D("News");
        $list= $news->where("ntype=1")->select();
        $count= $news->where("ntype=1")->field('id')->select();
        if(I('get.id')=='' ||   !is_numeric(I('get.id')) ){
            $about= $news->where("id=" .$list[0][id])->select();
            $this->assign('id', $list[0][id]);

        }else{
            $about= $news->where("id=" .I('get.id'))->select();
        }


        foreach($list as $key => $val){

            if(I('get.id') == $val["id"]){
                $this->assign('id', $val["id"]);
            }else{

            }

        }

        if(session('id') !== null){
            $this->assign('is_log', session('id'));
        }


        $name= __FUNCTION__; $this->assign('name', $name);
        $this->assign('list', $list);
        $this->assign('a',  $html);
        $this->assign('about', $about);
        $this->display();
    }

    //联系我们
    public function contact(){

        if(session('id') !== null){
            $this->assign('is_log', session('id'));
        }

        $name= __FUNCTION__; $this->assign('name', $name);
        $this->display();
    }

    //招聘中心
    public function recruit(){

        $url = $_SERVER["REQUEST_URI"];
        $url =  urldecode($url);
        $urlValue = explode( '?', $url );
        $selectValue = explode( '&', $urlValue[1]);
        $where = 1;
        if($urlValue[1]){
            foreach($selectValue as $key => $value ){
                $v = explode( '=', $value);
                if($v[0] == "create_time"){
                    if($v[1] == "今天"){
                        $where .= " and ".$v[0]." > ".strtotime(date("Y-m-d"));
                    }else if($v[1] == "最近三天"){
                        $where .= " and ".$v[0]." > ".strtotime(date("Y-m-d",strtotime("-3 day")));
                    }else if($v[1] == "最近一周"){
                        $where .= " and ".$v[0]." > ".strtotime(date("Y-m-d",strtotime("last Monday")));
                    }else{
                        $where .= " and ".$v[0]." > ".strtotime(date("Y-m-d",strtotime("last month")));
                    }
                }else if($v[0] != "page"){
                    if($v[0]){
                        $where .= " and ".$v[0]." like '".$v[1]."'";
                    }else{
                        continue;
                    }
                }

                if(strpos($value, "page") || strpos($value, "page")===0){
                    $page = $v[1];
                    $basePath = str_replace ($value ,"" ,$urlValue[1] );
                    $basePath = urlencode($basePath."&page=");
                }else{
                    $page = 1;
                    $basePath = urlencode($urlValue[1]."&page=");
                }
            }

        }else{
            $page = 1;
            $basePath = urlencode($urlValue[1]."page=");
        }

        $position= D("Positions");
        $list= $position->order('update_time desc')->where($where)->limit(($page-1)*6,6)->select();
        $count= $position->where($where)->count();

        //截取时间字符串
        foreach ($list as $key => $value){

            $list[$key]['update_time']= substr($value['update_time'], 0, 10);
            $list[$key]['xzfl']= substr($value['xzfl'], 0, 27);
            $list[$key]['xzfl']=rtrim($list[$key]['xzfl'], '，');
        }

        $name= __FUNCTION__;
        $this->assign('name', $name);
        $this->assign('list', $list);
        $this->assign('count', ceil($count/6));
        $this->assign('page', $page);
        $this->assign('basePath', $basePath);
        $this->display();
    }

    //招聘详情
    public function recruit_detail(){

        $position= D("Positions");
        $list= $position->order('update_time desc')->limit(6)->select();
        $count= $position->count();

        if(I('get.id')=='' || !is_numeric(I('get.id')) ){
            $posi= $position->where("pos_id=".$list[0]["pos_id"] )->select();
            //截取时间字符串
            foreach ($posi as $key => $value){
                $posi[$key]['update_time']= substr($value['update_time'], 0, 10);
            }
        }else{
            $posi= $position->where('pos_id=' .I('get.id') )->select();
            //截取时间字符串
            foreach ($posi as $key => $value){
                $posi[$key]['update_time']= substr($value['update_time'], 0, 10);
            }
        }
        if(session('id')){
            $Model=M('Position_apply_recorder');
            if($Model->where(" user_id= ".session('id')." and pos_id= ".I('get.id'))->getField("record_id")){
                $posi[0]["is_apply"] = '<input type="button" style="background: #ccc;" value="已申请" />';
            }else{
                $posi[0]["is_apply"] = '<form method="post" action="/position_apply"><input type="hidden" name="position_id" value="'.$posi[0]["pos_id"].'" /><input type="submit" value="立即申请" /></form>';
            }
        }else{
            $posi[0]["is_apply"] = '<form method="post" action="/position_apply"><input type="hidden" name="position_id" value="'.$posi[0]["pos_id"].'" /><input type="submit" value="立即申请" /></form>';
        }
        $name= "recruit";
        $this->assign('name', $name);
        $this->assign('posi' , $posi);
        $this->assign('list' , $list);
        $this->display();
    }

    //职位申请
    public function position_apply(){
        if($_SESSION['islogin']== '1'){
            $apply_record["user_id"]=session('id');
            $apply_record["pos_id"] = I("position_id");
            $apply_record["apply_time"] = time();
            $Model=M('Position_apply_recorder');
            if(!$Model->where("user_id=".$apply_record["user_id"]." and pos_id=".$apply_record["pos_id"])->getField("record_id")){
                $Model->add($apply_record);
                $this->success("申请成功", __APP__ . "/index.php/user_apply");
            }else{
                $this->error("不能重复申请!", __APP__ . "/index.php/recruit_detail?id=".$apply_record["pos_id"]);
            }

        }else{
            $this->error("请登入!", __APP__ . "/index.php/login");
        }
    }

    //登录
    public function login(){

        //判断是否登录
        if($_SESSION['islogin']=='1'){

            $this->success("已登入!", __APP__ . "/index.php/user_resume");
            exit();
        }


        $verify = new \Think\Verify();

        if(I('Post.login')== 'login'){

            if($verify->check( I('Post.code'))){
                $u=I('Post.username');
                $p=I('Post.password');
                $data['email']=$u;
                $data['password']=md5($p);   //md5加密
                $m=M('Users');
                $list=$m->where($data)->find();

                if($list){
                    if ($list['is_show'] == '0' ) {
                        $this->error('该账号异常请联系管理员');
                    }else{
                        $_SESSION['islogin']=1;           //登陆状态存入session
                        $_SESSION['username']=$list['username'];  //把用户名存入session
                        $_SESSION['id']=$list['id'];   //把用户id存入session
                        $this->success("登入成功!", __APP__ . "/index.php/user_resume");
                        exit();
                    }

                    if (I('Post.checkbox')=='checkbox') {

                        cookie('username',$u,30*24*3600);
                    }

                }else{
                    $this->error("用户名或密码不正确",'javascript:history.back(-1);',2);
                }

            }else{
                $this->error("验证码不正确",'javascript:history.back(-1);',2);
            }
        }


        if (cookie('username')){
            $this->assign('username', cookie('username'));
        }

        $this->display();
    }
    
    public function logout(){
    	session_destroy();
    	echo "<script>window.location.href='".__APP__."/index.php/login';</script>";exit;
    }

    //微信登录
    public function wxlogin(){

        //判断是否登录
        if($_SESSION['islogin']=='1'){

            $this->success("已登入!", __APP__ . "/wxindex");
            exit();
        }


        $verify = new \Think\Verify();

        if(I('Post.login')== 'login'){

//            if($verify->check( I('Post.code'))){
                $u=I('Post.username');
                $p=I('Post.password');
                $data['email']=$u;
                $data['password']=md5($p);   //md5加密
                $m=M('Users');
                $list=$m->where($data)->find();

                if($list){
                    if ($list['is_show'] == '0' ) {
                        $this->error('该账号异常请联系管理员');
                    }else{
                        $_SESSION['islogin']=1;           //登陆状态存入session
                        $_SESSION['username']=$list['username'];  //把用户名存入session
                        $_SESSION['id']=$list['id'];   //把用户id存入session
                        $this->success("登入成功!", __APP__ . "/wxindex");
                        exit();
                    }

                    if (I('Post.checkbox')=='checkbox') {

//                        cookie('username',$u,30*24*3600);
                        setcookie('username', $u, 30*24*3600);
                    }

                }else{
                    $this->error("用户名或密码不正确",'javascript:history.back(-1);',2);
                }

//            }else{
//                $this->error("验证码不正确",'javascript:history.back(-1);',2);
//            }
        }


        if (cookie('username')){
            $this->assign('username', cookie('username'));
        }

        $this->display();
    }
    //用户中心
    public function user(){

        $this->display();
    }

    //用户工作经历
    public function user_exper(){

      if($_SESSION['islogin']== '1'){

          $users=D('Users');
          $user= $users->where('id=' .session('id'))->find();
          $this->assign('user', $user);

        $trainModel = D("UsersTrainning");
        $edu_trainnings = $trainModel->where("type=2 and user_id=".(int)$_SESSION['id'])->select();
        $this->assign('edu_trainnings',$edu_trainnings);
        $name= "user_resume";
        $this->assign('sidenav', $name);
        $name= "user_resume";   $this->assign('sidenav', $name);
        $this->display();
      }else{
          $this->error("请登入!", __APP__ . "/index.php/login");
      }
    }

    //新增用户工作经历
    public function add_user_exper(){

      if($_SESSION['islogin']== '1') {

        if(!empty($_POST)){
            $trainModel = D("UsersTrainning");
            $_POST['type'] = 2;
            $_POST['user_id'] = $_SESSION['id'];
            $_POST['start'] = $_POST['start_year']."/".$_POST['start_month'];
            $_POST['end'] = $_POST['end_year']."/".$_POST['end_month'];
            $tid= $trainModel->add($_POST);
            if($tid){
                echo "<script>window.location.href='".__APP__."/index.php/user_exper/';</script>";exit;
            }
        }
      }else{
          $this->error("请登入!", __APP__ . "/index.php/login");
      }
    }

    //修改工作经历
    public function user_exper_edit(){

        if($_SESSION['islogin']== '1'){

        $trainModel = D("UsersTrainning");
        if(!empty($_POST)){
            $_POST['start'] = $_POST['start_year']."/".$_POST['start_month'];
            $_POST['end'] = $_POST['end_year']."/".$_POST['end_month'];
            $result = $trainModel->where("train_id=".(int)$_POST['train_id'])->save($_POST);
            if($result){
                echo "<script>window.location.href='".__APP__."/index.php/user_exper/';</script>";exit;
            }
        }else{
            $train_id = (int)$_GET['id'];
            $edu_trainning = $trainModel->where("type=2 and user_id=".(int)$_SESSION['id']." and train_id=".$train_id)->find();
            $this->assign('vo',$edu_trainning);
            $name= "user_resume";   $this->assign('sidenav', $name);
            $this->display();
           }
        }else{
            $this->error("请登入!", __APP__ . "/index.php/login");
        }
    }

    //用户所受教育经历
    public function user_edu(){

     if($_SESSION['islogin']== '1'){

         $users=D('Users');
         $user= $users->where('id=' .session('id'))->find();
         $this->assign('user', $user);


        $trainModel = D("UsersTrainning");
        $edu_trainnings = $trainModel->where("type=1 and user_id=".(int)$_SESSION['id'])->select();
        $this->assign('edu_trainnings',$edu_trainnings);
        $name= "user_resume";   
        $this->assign('sidenav', $name);
        $this->display();
     }else{
         $this->error("请登入!", __APP__ . "/index.php/login");
     }
    }
    
    //新增用户教育经历
    public function add_user_edu(){

      if($_SESSION['islogin']== '1'){

    	if(!empty($_POST)){
    		$trainModel = D("UsersTrainning");
    		$_POST['type'] = 1;
    		$_POST['user_id'] = $_SESSION['id'];
    		$_POST['start'] = $_POST['start_year']."/".$_POST['start_month'];
    		$_POST['end'] = $_POST['end_year']."/".$_POST['end_month'];
    		$tid= $trainModel->add($_POST);
    		if($tid){
                echo "<script>window.location.href='".__APP__."/index.php/user_edu/';</script>";exit;
    		}
    	   }
        }else{
            $this->error("请登入!", __APP__ . "/index.php/login");
        }
    }
    
    //删除教育经历
    public function del_trainning(){

    if($_SESSION['islogin']== '1'){

       $train_id = (int)$_POST['train_id'];
       $trainModel = D("UsersTrainning");
       $result = $trainModel->where("train_id=".$train_id)->delete();
       if($result){
       	echo $result;
       }
      }else{
        $this->error("请登入!", __APP__ . "/index.php/login");
     }
    }
    
    //修改教育经历
    public function user_edu_edit(){

     if($_SESSION['islogin']== '1') {

         //查询用户信息显示在左侧
         $users=D('Users');
         $user= $users->where('id=' .session('id'))->find();
         $this->assign('user', $user);

    	$trainModel = D("UsersTrainning");
    	if(!empty($_POST)){
    		$_POST['start'] = $_POST['start_year']."/".$_POST['start_month'];
    		$_POST['end'] = $_POST['end_year']."/".$_POST['end_month'];
    	    $result = $trainModel->where("train_id=".(int)$_POST['train_id'])->save($_POST);
    		if($result){
                echo "<script>window.location.href='".__APP__."/index.php/user_edu/';</script>";exit;
    		}
    	}else{
    		$train_id = (int)$_GET['id'];
    		$edu_trainning = $trainModel->where("type=1 and user_id=".(int)$_SESSION['id']." and train_id=".$train_id)->find();
    		$this->assign('vo',$edu_trainning);
            $name= "user_resume";   $this->assign('sidenav', $name);
    		$this->display();
    	}
     }else{
         $this->error("请登入!", __APP__ . "/index.php/login");
     }
    }

    //用户所受的培训
    public function user_train(){

     if($_SESSION['islogin']== '1'){

         $users=D('Users');
         $user= $users->where('id=' .session('id'))->find();
         $this->assign('user', $user);

        $trainModel = D("UsersTrainning");
        $edu_trainnings = $trainModel->where("type=3 and user_id=".(int)$_SESSION['id'])->select();
        $this->assign('edu_trainnings',$edu_trainnings);
        $name= "user_resume";
        $this->assign('sidenav', $name);
        $name= "user_resume";   $this->assign('sidenav', $name);
        $this->display();
      }else{
         $this->error("请登入!", __APP__ . "/index.php/login");
      }
    }

    //新增用户培训
    public function add_train_exper(){
        if(!empty($_POST)){
            $trainModel = D("UsersTrainning");
            $_POST['type'] = 3;
            $_POST['user_id'] = $_SESSION['id'];
            $_POST['start'] = $_POST['start_year']."/".$_POST['start_month'];
            $_POST['end'] = $_POST['end_year']."/".$_POST['end_month'];
            $tid= $trainModel->add($_POST);
            if($tid){
                echo "<script>window.location.href='".__APP__."/index.php/user_train/';</script>";exit;
            }
        }
    }

    //修改培训经历
    public function user_train_edit(){

      if($_SESSION['islogin']== '1'){

          //查询用户信息显示在左侧
          $users=D('Users');
          $user= $users->where('id=' .session('id'))->find();
          $this->assign('user', $user);

            $trainModel = D("UsersTrainning");
        if(!empty($_POST)){
            $_POST['start'] = $_POST['start_year']."/".$_POST['start_month'];
            $_POST['end'] = $_POST['end_year']."/".$_POST['end_month'];
            $result = $trainModel->where("train_id=".(int)$_POST['train_id'])->save($_POST);
            if($result){
                echo "<script>window.location.href='".__APP__."/index.php/user_train/';</script>";exit;
            }
          }else{
            $train_id = (int)$_GET['id'];
            $edu_trainning = $trainModel->where("type=3 and user_id=".(int)$_SESSION['id']." and train_id=".$train_id)->find();
            $this->assign('vo',$edu_trainning);
            $name= "user_resume";   $this->assign('sidenav', $name);
            $this->display();
          }

      }else{
          $this->error("请登入!", __APP__ . "/index.php/login");
        }
    }

    //用户求职意向
    public function user_intention(){

        $user_job= D('UsersJobs');
        $users= D('Users');
        $id= $users->where('id='.session('id'))->find();
        if($_SESSION['islogin']== '1'){

            $users=D('Users');
            $user= $users->where('id=' .session('id'))->find();
            $this->assign('user', $user);

          $job= $user_job->where('user_id=' .session('id'))->find();
            if($job['user_id']==''){
                 $data['user_id']= session('id');
                 $user_job->data($data)->add();

            }else{

              if(I('post.ID')) {
                  $user_job->create();
                  $_POST['user_id']= session('id');
                  $num= $user_job->where('user_id='.session('id'))->save($_POST);  

                  if($num){
                      header("Content-type: text/html; charset=utf-8");
                      echo "<script>window.location.href='".__APP__."/index.php/user_resume/';</script>";exit;
                  }
              }
            }

        }else{
            $this->error("请登入!", __APP__ . "/index.php/login");
        }

        $name= "user_resume";   $this->assign('sidenav', $name);
        $this->assign('ID', $id);
        $this->assign('job', $job);
        $this->display();
    }

    //用户个人简历
    public function user_resume(){

     if($_SESSION['islogin']== '1'){
         $users_jobs= D('UsersJobs');
         $users= D('Users');
         $user=$users->where('id='.session('id'))->find();
         $job=$users_jobs->where('user_id='.session('id'))->find();

         $train= D("UsersTrainning");
         $train_edu= $train->where('type=1 and user_id='.session('id'))->select();
         $train_exper= $train->where('type=2 and user_id='.session('id'))->select();
         $train_train= $train->where('type=3 and user_id='.session('id'))->select();

         $name= __FUNCTION__;   $this->assign('sidenav', $name);

         $this->assign('user', $user);
         $this->assign('edu', $train_edu);
         $this->assign('exper', $train_exper);
         $this->assign('train', $train_train);
         $this->assign('job', $job);
         $this->display();
        }else{

//         $this->error("请登入!", __APP__ . "/index.php/login");
           $this->redirect('Index.php/login');
      }

    }

    //用户申请记录
    public function user_apply(){
        header("Content-type:text/html;charset=utf-8");

        $users=D('Users');
        $user= $users->where('id=' .session('id'))->find();
        $this->assign('user', $user);

        //echo time();exit;
        if($_SESSION['islogin']== '1'){
            $user_id=session('id');
            $page=intval(I('get.page'));
            if($page==0){
                $page=1;
            }

            //获取文章列表及分页
            $where='where user_id='.$user_id;
            $perpage=10;
            $start=($page-1)*$perpage;
            $sql="select a.*,b.* from `_web_position_apply_recorder` a left join `_web_positions` b on a.pos_id=b.pos_id ".$where." order by a.record_id desc limit $start,$perpage";
            //echo $sql;exit;
            $Model=M('Position_apply_recorder');
            $article_list=$Model->query($sql);
            //dump($article_list);exit;
            $query=$Model->query("select count(record_id) as total from `_web_position_apply_recorder` ".$where." ");
            $article_total=$query[0]['total'];
            $page_total=ceil($article_total/$perpage);  //分页总数
            $this->assign("article_list", $article_list);
            $this->assign("page",$page);
            $this->assign("count",$page_total);

            $name= __FUNCTION__;
            $this->assign('sidenav', $name);
            $this->display();

        }else{

            $this->error("请登入!", __APP__ . "/index.php/login");
        }
    }

    //用户资料修改
    public function user_modify(){

        if($_SESSION['islogin']== '1'){

            $users= D('Users');
            $user=$users->where('id='.session('id'))->find();

            $date=explode('-', $user['birthday']);

            if(I('post.ID')){
                $_POST['birthday'] =I('post.year').'-'.I('post.month').'-'.I('post.date');
                $_POST['age']= intval(date('Y'))- intval(I('post.year'));
                $users->create();
                $count=$users->where('id=' .I('post.ID'))->save($_POST);

                if($count){
                    header("Content-type: text/html; charset=utf-8");
                    echo "<script>window.location.href='".__APP__."/index.php/user_resume/';</script>";exit;
                }

            }

            if(I('get.del')){

                $data['head_img']='';
                $users->where('id='.I('get.del'))->save($data);
            }

            $name= __FUNCTION__;   $this->assign('sidenav', $name);
            $this->assign('user', $user);
            $this->assign('date', $date);
        }else{

            $this->error("请登入!", __APP__ . "/index.php/login");
        }

        $name= "user_resume";   $this->assign('sidenav', $name);
        $this->display();
    }

    //用户投递反馈
    public function user_back(){
        header("Content-type:text/html;charset=utf-8");
        //echo time();exit;
        if($_SESSION['islogin']== '1'){
            $user_id=session('id');
            $page=intval(I('get.page'));
            if($page==0){
                $page=1;
            }

            $users=D('Users');
            $user= $users->where('id=' .session('id'))->find();
            $this->assign('user', $user);


            //获取文章列表及分页
            $where='where user_id='.$user_id;
            $perpage=10;
            $start=($page-1)*$perpage;
            $sql="select a.*,b.* from `_web_position_apply_recorder` a left join `_web_positions` b on a.pos_id=b.pos_id ".$where." order by a.record_id desc limit $start,$perpage";
            //echo $sql;exit;
            $Model=M('Users_jobs');
            $article_list=$Model->query($sql);
            //dump($article_list);exit;
            $query=$Model->query("select count(record_id) as total from `_web_position_apply_recorder` ".$where." ");
            $article_total=$query[0]['total'];
            $page_total=ceil($article_total/$perpage);  //分页总数
            $this->assign("article_list", $article_list);
            $this->assign("page",$page);
            $this->assign("count",$page_total);


            $name= __FUNCTION__;
            $this->assign('sidenav', $name);
            
        }else{
            $this->error("请登入!", __APP__ . "/index.php/login");
        }

        $users=D('Users');
        $user= $users->where('id=' .session('id'))->find();
        $this->assign('user', $user);

        $name= __FUNCTION__;   $this->assign('sidenav', $name);
        $this->display();

    }

    //用户自我评价
    public function user_evaluate(){
    	$userModel = D("Users");
        if(!empty($_POST)){
        	$result = $userModel->where("id=".(int)$_POST['id'])->save(array('user_memo'=>$_POST['user_memo']));
        	if($result){
        		echo "<script language=\"javascript\">window.location.href='".__APP__."/index.php/user_resume/';</script>";
        	}
        }else{
        	$name= "user_resume";   $this->assign('sidenav', $name);
        	$user = $userModel->where("id=".(int)$_SESSION['id'])->find();
        	$this->assign("user_id",$user['id']);
        	$this->assign("user_memo",$user['user_memo']);
        	$this->display();
        }
        
    }

    //用户注册
    public function user_register(){
        if(!empty($_POST)){
        	$userModel = D("Users");
        	$_POST['password'] = md5($_POST['password']);
        	$_POST['create_time'] = date('Y-m-d H:i:s');
        	$_POST['is_show'] = 1;
        	$seed = $userModel->random(6);
        	$_POST['auth_code'] = $seed;
        	$uid = $userModel->add($_POST);
            if(!empty($uid)){
            	//发送验证邮件
            	import ( 'WebAdmin.ORG.Mail' );
            	require APP_PATH."/Home/Conf/smtp.php";
            	$content = "欢迎加入龙塘人力资源\n".'您的邮件帐户是：'.$_POST['email']."\n密码是:".$_POST['confirm']."\n点击下面链接验证邮件\n<a href='http://".$_SERVER['HTTP_HOST']."/index.php/verify_email/code/".md5($_POST['auth_code'])."/id/".$uid."'>点击前往</a>";
            	SendMail($_POST['email'],'欢迎加入龙塘人力资源网站',$content);
            	header("Content-type: text/html; charset=utf-8");
            	echo "<script language=\"\">alert('用户注册成功，请完善您的个人信息!');window.location.href='".__APP__."/index.php/user_modify/';</script>";
            }else{
            	$this->error('数据提交错误！');
            }
        }else{
        	$this->display();
        }
    }
    
    //验证邮件所属
    public function verify_email(){
    	$code = $_GET['code'];
    	$id = $_GET['id'];
    	$userModel = D("Users");
    	$user = $userModel->where("id=".(int)$id)->select();
    	$result = false;
    	if($user[0]['id']){
    		if($user[0]['auth_code']){
    		  if(md5($user[0]['auth_code'])==$code){
    		  	$seed = $userModel->random(6);
    		  	$userModel->where("id=".(int)$id)->save(array('auth'=>1,'auth_code'=>$seed));
    		  	$result = true;
    		  }
    		}
    	}
    	header("Content-type: text/html; charset=utf-8");
    	if($result){
    		echo "<script language=\"\">alert(\"邮箱已验证成功\");window.location.href='/user_resume';</script>";
    	}else{
    		echo "<script language=\"\">alert(\"邮箱已验证失败\");window.location.href='/user_resume';</script>";
    	}
    	
    }
    
    //验证邮件帐号是否存在
    public function check_email(){
    	$email = trim($_POST['email']);
    	if(!preg_match("/^[\w\-\.]+@[\w\-]+(\.\w+)+$/",$email)){
    		echo "<span style='color:red'>邮箱格式不正确!</span>";
    	}else{
    	$userModel = D("Users");
    	$check = $userModel->where("email='".addslashes($_POST['email'])."'")->select();
    	if(!empty($check[0]['id'])){
    		echo "<span style='color:red'>邮箱已存在，请更换邮箱</span>";
    	}else{
    		echo "<span style='color:green'>邮箱可用</span>";
    	}
    	}
    }

    //用户修改密码
    public function user_pass(){
       $name= __FUNCTION__;   $this->assign('sidenav', $name);

        $users=D('Users');
        $user= $users->where('id=' .session('id'))->find();
        $this->assign('user', $user);


        if(!empty($_POST)){

       	  $verify = new \Think\Verify();
       	  if($_POST['confirm']==$_POST['password'] && $verify->check( I('Post.code'))){
       	   $userModel = D("Users");
       	   $user = $userModel->where("id=".(int)$_POST['id'])->find();
       	   
       	   if($user['mobile']==$_POST['mobile'] && $user['password']==md5($_POST['old-password'])){
       	   	 $result = $userModel->where("id=".(int)$_POST['id'])->save(array('password'=>md5($_POST['password'])));
       	   	 if($result){
       	   	 	echo "<script language=\"javascript\">alert('密码修改成功');window.location.href='/user_resume';</script>";
       	   	 }
       	   }else{
       	   	 $this->error("旧密码或手机号码不正确",'javascript:history.back(-1);',2);
       	   }
       	  }else{
       	  	 $this->error("密码不一致或验证码不正确",'javascript:history.back(-1);',2);
       	  }
       	   
       }else{
       	 $this->assign('user_id',$_SESSION['id']);
       }
        $this->display();
    }

    //用户找回密码
    public function user_find_pass(){
        if(!empty($_POST)){
        	header("Content-type: text/html; charset=utf-8");
        	if($_POST['password']!=$_POST['confirm']){
        		echo "<script language=\"javascript\">alert('密码与确认不一致!');history.go(-2);</script>";exit;
        	}
        	if($_POST['code']==$_SESSION['passwd_seed']){
        		$userModel = D("Users");
        		$result = $userModel->where("email='".$email."'")->save(array('password'=>md5($_POST['password'])));
        		if($result){
        			session_destory();
        			echo "<script language=\"javascript\">alert('密码修改成功!');window.location.href='/login';</script>";exit;
        		}
        	}else{
        		echo "<script language=\"javascript\">alert('验证码不正确!');history.go(-2);</script>";exit;
        	}
        	
        }else{
        	$this->display();
        }
    }
    
    //发送邮箱验证码
    public function send_email_code(){
    	if(!empty($_POST['email'])){
    	  $email = trim($_POST['email']);	
    	  $userModel = D("Users");
    	  $check = $userModel->where("email='".$email."'")->find();
    	  if($check){
    	    $seed = $userModel->random(4);
    	    $_SESSION['passwd_seed'] = $seed;
    	    import ( 'WebAdmin.ORG.Mail' );
    	    require APP_PATH."/Home/Conf/smtp.php";
    	    $content = "您在龙塘人力资源的邮件验证码是：".$seed."";
    	    SendMail($_POST['email'],'龙塘人力资源网站验证码',$content);
    	    echo "验证码已发送,请查收您的邮箱!";
    	  }else{
    	  	echo "邮箱不存在,请注册";
    	  }
    	}
    }
    
    //HR留言板
    public function hr_board(){
       if(!empty($_POST)){
       	 $contactModel = D("Contact");
       	 $_POST['ip'] = get_client_ip();
       	 $_POST['add_time'] = date('Y-m-d H:i:s');
       	 $cid = $contactModel->add($_POST);
       	 if($cid){
       	 	 //发送留言到指定邮箱
       	 	$emailModel = D("Sliders");
       	 	$email = $emailModel->where("ntype=4")->find();
       	 	$send_email = $email['title'];
       	 	import ( 'WebAdmin.ORG.Mail' );
       	 	require APP_PATH."/Home/Conf/smtp.php";
       	 	$content = file_get_contents(APP_PATH."/Home/View/contact_email.html");
       	 	$content = str_replace('__URL__',$_SERVER['HTTP_HOST']."/",$content);
       	 	$content = str_replace('{$vo.request}',$_POST['request'],$content);
       	 	$content = str_replace('{$vo.content}',$_POST['content'],$content);
       	 	$content = str_replace('{$vo.company}',$_POST['company'],$content);
       	 	$content = str_replace('{$vo.name}',$_POST['name'],$content);
       	 	$content = str_replace('{$vo.tel}',$_POST['tel'],$content);
       	 	$content = str_replace('{$vo.email}',$_POST['email'],$content);
       	 	SendMail($send_email,'你有新的留言',$content);
       	 	echo "<script >alert('您的留言已成功提交,请耐心等待我们的回复！');window.location.href='".__APP__."/index.php';</script>";
       	 }else{
       	   	echo "<script language=\"\">alert('数据出错，请重新提交！');history.go(-1);</script>";
       	 }
       }else{
        $this->display();
       }
    }

    //HR专栏列表
    public function hr_list(){

        $news=D('News');
        $tit= D("NewsCategory");

        $title=$tit->where('ntype=5 and is_show=1 ')->order('id asc ')->select();

        $this->assign('id', $title[0]['id']) ;
        if(I('get.id')==''){
            $one=$tit->where('id='.$title[0]['id'] )->find();
//            $new=$news->where('is_show=1 and category_id=' .$one['id'])->limit(3)->select();

            $where = "is_show=1 and category_id=". $one['id'];
            $count = $news->where($where)->count();
            $pagecount = 3;
            $app= __APP__.'/index.php/hr_list/id/'.$one['id'];
            $new=$this->getPage($count, $pagecount, $news, $where, $app);

            foreach($new as $key => $val){

                //$new[$key]['content'] = htmlspecialchars($new[$key]['content']);
                $new[$key]['content'] = preg_replace( "/<(.*?)>/si", "", $new[$key]['content'] ).'...';
                $new[$key]['content'] = mb_substr($new[$key]['content'],0,35,'utf-8');
                //标题字符串截取
                $str_len= mb_strlen($new[$key]['title'],'utf-8');
                if($str_len>40){
                    $new[$key]['title'] = mb_substr($new[$key]['title'],0,35,'utf-8').'...';
                }

//                $imgurl= $new[$key]['imgurl'];
//                $imgrul= explode("/", $imgurl);
//                $new[$key]['imgurl']= './'.$imgrul[3].'/'.$imgrul[4].'/'.$imgrul[5];


            }
            $this->assign('one', $one);
            $this->assign('new', $new);
        }else{
            foreach($title as $key => $val){

                       if(I('get.id') == $val["id"]){
                       $this->assign('id', $val["id"]);
                       $one=$tit->where('id='.I('get.id'))->find();
//                       $new=$news->where('is_show=1 and category_id=' .$one['id'])->limit(3)->select();

                       $where = " is_show=1 and category_id=". I('get.id');
                       $count = $news->where($where)->count();
                       $pagecount = 3;
                       $app= __APP__.'/index.php/hr_list/id/'.$one['id'];
                       $new=$this->getPage($count, $pagecount, $news, $where, $app);



                }else{

                   }
               }


            foreach($new as $key => $val){

//                $imgurl= $new[$key]['imgurl'];
//                $imgrul= explode("/", $imgurl);
//                $new[$key]['imgurl']= './'.$imgrul[3].'/'.$imgrul[4].'/'.$imgrul[5];

                $new[$key]['content'] = preg_replace( "/<(.*?)>/si", "", $new[$key]['content'] );
                $new[$key]['content'] = mb_substr($new[$key]['content'],0,35,'utf-8');
                $str_len= mb_strlen($new[$key]['title'],'utf-8');
                if($str_len>40){
                    $new[$key]['title'] = mb_substr($new[$key]['title'],0,35,'utf-8').'...';
                }
            }

        }

        if(session('id') !== null){
            $this->assign('is_log', session('id'));
        }


        $name= __FUNCTION__; $this->assign('name', $name);
        $this->assign('title', $title);
        $this->assign('one', $one);
        $this->assign('new', $new);
        $this->display();
    }

    //HR下载专区
    public function hr_download(){

        $tit= D("NewsCategory");
        $downloads= D('Downloads');
        $title=$tit->where('ntype=11 and is_show=1')->order('id asc ')->select();

        if(I('get.id')==''){

            $where = " is_show=1 and category_id=". $title[0]['id'];
            $count = $downloads->where($where)->count();
            $pagecount = 5;
            $app= __APP__.'/index.php/hr_download/id/'.$title[0]['id'];
            $down= $this->getPage($count, $pagecount, $downloads, $where, $app);

            foreach ($down as $key => $value){

                $str_len= mb_strlen( $down[$key]['title'],'utf-8');
                if($str_len>25){
                    $down[$key]['title'] = mb_substr( $down[$key]['title'],0,25,'utf-8').'...';
                }
                $down[$key]['create_time']= substr($value['create_time'], 0, 10);


            }
            $this->assign('down', $down);

        }else{

//
            $where = " is_show=1 and category_id=". I('get.id');
            $count = $downloads->where($where)->count();
            $pagecount = 5;
            $app= __APP__.'/index.php/hr_download/id/'.I('get.id');
            $down= $this->getPage($count, $pagecount, $downloads, $where, $app);

            foreach ($down as $key => $value){

                $down[$key]['create_time']= substr($value['create_time'], 0, 10);
            }


        }

        $this->assign('id', $title[0]['id']) ;

        if(I('get.id')==''){
            $one=$tit->where('id='.$title[0]['id'] )->find();
            $this->assign('one', $one);
        }else {

            foreach ($title as $key => $val) {

                if (I('get.id') == $val["id"]) {

                    $this->assign('id', $val["id"]);
                    $one=$tit->where('id='.I('get.id'))->find();
                    $this->assign('one', $one);

                } else {

                }
            }

            $this->assign('down', $down);
        }

        if(session('id') !== null){
            $this->assign('is_log', session('id'));
        }


        $name= __FUNCTION__; $this->assign('name', $name);
        $this->assign('title', $title);
        $this->display();
    }

    //下载文件
    public function hr_file(){

       $downloads= D('Downloads');
       $users= D('Users');

       $root_path = dirname(dirname(dirname(dirname(__FILE__))));

       $FilePath= './Public/Uploads/';
//       $FilePath= '';
       $down= $downloads->where('down_id='.I('get.id'))->find();

        if(!isset($_SESSION['islogin'])){
            if($down['groups']==1){

                $down['savename']= explode("/", $down['savename']);
                $down['savename']=$down['savename'][count($down['savename'])-1];
                $filename= $FilePath.$down['savename'];
                $this->download($filename,$show_name='');  exit();

            }else{
                $this->error('请登录，下载此文件',  __APP__."/index.php/login");
            }
        }


        if($_SESSION['islogin']== '1'){
            $user= $users->where('id='.session('id'))->find();

              if($user['groups']>=$down['groups']){

                  $down['savename']= explode("/", $down['savename']);
                  $down['savename']=$down['savename'][count($down['savename'])-1];
                  $filename= $FilePath.$down['savename'];
                  $this->download($filename,$show_name='');  exit();

//                  $filename= $FilePath.$down['savename'];
//                  $this->download($filename,$show_name='');  exit();

              }else{
                  $this->error('您无权限下载此文件，请联系管理员');
              }

        }else{
            $this->error("请登入!",  __APP__ . "/index.php/login");
        }







//           $filename=  './Public/Uploads/57b16a29f2c83.docx';
//           $this->download($filename,$show_name='');



    }

    //下载专区详情
    public function hr_down_detail(){

        $downloads= D('Downloads');
        $news= D('NewsCategory');
        $list= $downloads->where('is_show=1')->order('create_time desc')->limit(6)->select();

        foreach ($list as $key=>$value){
            $str_len= mb_strlen($list[$key]['name'],'utf-8');
            if($str_len>25){
                $list[$key]['name'] = mb_substr($list[$key]['name'],0,25,'utf-8').'...';
            }
        }

        if (I('get.id')=='') {

            $this->error('非法登录！');

        }else{

            if(!is_numeric(I('get.id'))){
                echo "<script>history.back(-1);</script>";
            }

            $down= $downloads->where('down_id=' .I('get.id'))->find();
            $new=$news->where('id='.$down['category_id'])->find();

            //上一条
            $map['is_show']= '1';
            $map['category_id']= $down['category_id'];
            $map['down_id']  = array('lt', I('get.id'));
            $data_pre = $downloads->where($map)->order('down_id desc')->find();
            $newOrderid=$data_pre['down_id'];




            //点击跳转到上一条
            if(I('get.action')=='pre'){

                $map['is_show']= '1';
                $map['down_id']  = array('lt', I('get.id'));
                $data = $downloads->where($map)->order('down_id desc')->find();

                if(count($data)>0){
//                    $newOrderid = $data['down_id']+1;
                    //跳转到上一条表页
                    $this->redirect('hr_down_detail'."/id/".I('get.id'));
                }else{
                    $this->assign('ee' ,'已是第一条');
                }
            }


            //下一条
            $map['is_show']= '1';
            $map['category_id']= $down['category_id'];
            $map['down_id']  = array('gt', I('get.id'));
            $data_next = $downloads->where($map)->order('down_id asc')->find();
            $newOrderid=$data_pre['down_id'];



            if(empty($data_pre)){
                $this->assign('pre', '已是最后一条');
            }else{
                $this->assign('pre', $data_pre['title']);
            }

            //点击跳转到下一条
            if(I('get.action')=='next'){

                $map['is_show']= '1';
                $map['down_id']  = array('gt', I('get.id'));
                $data = $downloads->where($map)->order('down_id asc')->find();

                if(count($data)>0){
//                    $newOrderid = $data['down_id']+1;
                    //跳转到上一条表页
                    $this->redirect('hr_down_detail'."/id/".I('get.id'));
                }else{
                    $this->assign('aa' ,'最后第一条');
                }
            }



        }

        $name= "hr_download"; $this->assign('name', $name);
        $this->assign('down', $down);
        $this->assign('new', $new);
        $this->assign('pre', $data_pre);
        $this->assign('list', $list);
        $this->assign('next', $data_next);
        $this->display();
    }

    //HR专栏详情
    public function hr_detail(){

        $news=D("News");
        $cate=D('NewsCategory');
        $new=$news->where('ntype=5 and is_show=1')->limit(6)->order('newsdt desc ')->select();

        foreach($new as $key => $val){

            $str_len= mb_strlen($new[$key]['title'],'utf-8');
            if($str_len>15){
                $new[$key]['title'] = mb_substr($new[$key]['title'],0,15,'utf-8').'...';
            }
        }


        if(I('get.id')=='' || !is_numeric(I('get.id')) ){

            $title=$news->where('id='.$new[0]['id'])->find();
            $cate_title= $cate->where('id='.$title['category_id'])->find();

            //上一条
            $map_pre['ntype']= '5';
            $map_pre['id']  = array('lt', $new[0]['id']);
            $data_pre = $news->where($map_pre)->order('id desc')->limit(1)->find();
            $str_len= mb_strlen($data_pre['title'],'utf-8');
            if($str_len>25){
                $data_pre['title'] = mb_substr($data_pre['title'],0,25,'utf-8').'...';
            }
            $this->assign('pre', $data_pre['title']);

            //下一条
            $map_next['ntype']= '5';
            $map_next['id']  = array('gt',$new[0]['id']);
            $data_next = $news->where($map_next)->order('orderid asc')->limit(1)->select();
            $str_len= mb_strlen($data_next['title'],'utf-8');
            if($str_len>25){
                $data_next['title'] = mb_substr($data_next['title'],0,25,'utf-8').'...';
            }
            $this->assign('next', $data_next['title']);

        }else{
            $title=$news->where('id='.I('get.id'))->find();
            $cate_title= $cate->where('id='.$title['category_id'])->find();

            //上一条
            $map_pre['ntype']= '5';
            $map_pre['id']  = array('lt', I('get.id'));
            $data_pre = $news->where($map_pre)->order('id desc')->limit(1)->find();
            if(empty($data_pre)){
                $this->assign('pre', '已是第一条');
            }else{
                $str_len= mb_strlen($data_pre['title'],'utf-8');
                if($str_len>25){
                    $data_pre['title'] = mb_substr($data_pre['title'],0,25,'utf-8').'...';
                }
                $this->assign('pre', $data_pre['title']);
            }

            //下一条
            $map_next['ntype']= '5';
            $map_next['id']  = array('gt', I('get.id'));
            $data_next = $news->where($map_next)->order('id asc')->limit(1)->find();
            if(empty($data_next)){
                $this->assign('next', '已是最后一条');
            }else{
                $str_len= mb_strlen($data_next['title'],'utf-8');
                if($str_len>25){
                    $data_next['title'] = mb_substr($data_next['title'],0,25,'utf-8').'...';
                }
                $this->assign('next', $data_next['title']);
            }

        }

        //点击跳转到上一条
        if(I('get.action')=='pre'){

            $map['ntype']= '5';
            $map['id']  = array('lt', I('get.id'));
            $data = $news->where($map)->order('id desc')->limit(1)->select();

            if(count($data)>0){
                $newOrderid = $data[0]['id'];
                //跳转到上一条表页
                $this->redirect('hr_detail'."/id/".$newOrderid);
            }
        }

        //点击跳转到下一条
        if(I('get.action')=='next'){

            $map['ntype']= '5';
            $map['id']  = array('gt', I('get.id'));
            $data = $news->where($map)->order('id asc')->limit(1)->select();

            if(count($data)>0){
                $newOrderid = $data[0]['id'];
                //跳转到上一条表页
                $this->redirect('hr_detail'."/id/".$newOrderid);
            }
        }



        $name= __FUNCTION__; $this->assign('name', 'hr_list');
        $this->assign('new', $new);
        $this->assign('title', $title);
        $this->assign('cate_title', $cate_title);
        $this->display();
    }

    //法律法规列表
    public function law(){

        $tit= D("NewsCategory");
        $news=D("News");
        $title=$tit->where('ntype=3 and is_show=1')->order('id asc ')->select();


        $this->assign('id', $title[0]['id']) ;

        if(I('get.id')==''){
            $one=$tit->where('id='.$title[0]['id'] )->find();
//            $list=$news->where('category_id='. $one['id'])->order('newsdt desc')->select();

            $where = " is_show=1 and category_id=". $one['id'];
            $count = $news->where($where)->count();
            $pagecount = 5;
            $app= __APP__.'/index.php/law/id/'.$one['id'];
            $list=$this->getPage($count, $pagecount, $news, $where, $app);

            foreach ($list as $key => $value){

                    $str_len= mb_strlen($list[$key]['title'],'utf-8');
                    if($str_len>25){
                      $list[$key]['title'] = mb_substr($list[$key]['title'],0,25,'utf-8').'...';
                    }

                $list[$key]['newsdt']= substr($value['newsdt'], 0, 10);
            }
            $this->assign('one', $one);
            $this->assign('list_law', $list);
        }else {

            foreach ($title as $key => $val) {

                if (I('get.id') == $val["id"]) {

                    $this->assign('id', $val["id"]);
                    $one=$tit->where('id='.I('get.id'))->find();
                    $new=$news->where('category_id='. I('get.id'))->select();

                    $where = " is_show=1 and category_id=". I('get.id');
                    $count = $news->where($where)->count();
                    $pagecount = 5;
                    $app= __APP__.'/index.php/law/id/'.$one['id'];
                    $new=$this->getPage($count, $pagecount, $news, $where, $app);


                    foreach ($new as $key => $value){

                        $str_len= mb_strlen($new[$key]['title'],'utf-8');
                        if($str_len>25){
                            $new[$key]['title'] = mb_substr($new[$key]['title'],0,25,'utf-8').'...';
                        }

                        $new[$key]['newsdt']= substr($value['newsdt'], 0, 10);
                    }
                    $this->assign('one', $one);
                    $this->assign('list_law', $new);

                } else {

                }
            }

        }

        if(session('id') !== null){
            $this->assign('is_log', session('id'));
        }

        $name= __FUNCTION__; $this->assign('name', $name);
        $this->assign('title', $title);
        $this->display();
    }

    //HR专栏详情
    public function law_detail(){

        $news=D("News");
        $cate=D('NewsCategory');
        $new=$news->where('ntype=3 and is_show=1')->limit(6)->order('newsdt desc ')->select();

        if(I('get.id')=='' || !is_numeric(I('get.id')) ){

            $title=$news->where('id='.$new[0]['id'])->find();
            $cate_title= $cate->where('id='.$title['category_id'])->find();

            //上一条
            $map_pre['ntype']= '3';
            $map_pre['id']  = array('lt', $new[0]['id']);
            $data_pre = $news->where($map_pre)->order('id desc')->limit(1)->find();
            $str_len= mb_strlen($data_pre['title'],'utf-8');
            if($str_len>15){
                $data_pre['title'] = mb_substr($data_pre['title'],0,15,'utf-8').'...';
            }
            $this->assign('pre', $data_pre['title']);

            //下一条
            $map_next['ntype']= '3';
            $map_next['id']  = array('gt',$new[0]['id']);
            $data_next = $news->where($map_next)->order('orderid asc')->limit(1)->select();
            $str_len= mb_strlen($data_next['title'],'utf-8');
            if($str_len>15){
                $data_next['title'] = mb_substr($data_next['title'],0,15,'utf-8').'...';
            }
            $this->assign('next', $data_next['title']);

        }else{
            $title=$news->where('id='.I('get.id'))->find();
            $cate_title= $cate->where('id='.$title['category_id'])->find();

            //上一条
            $map_pre['ntype']= '3';
            $map_pre['id']  = array('lt', I('get.id'));
            $data_pre = $news->where($map_pre)->order('id desc')->limit(1)->find();
            if(empty($data_pre)){
                $this->assign('pre', '已是第一条');
            }else{
                $str_len= mb_strlen($data_pre['title'],'utf-8');
                if($str_len>15){
                    $data_pre['title'] = mb_substr($data_pre['title'],0,15,'utf-8').'...';
                }
                $this->assign('pre', $data_pre['title']);
            }

            //下一条
            $map_next['ntype']= '3';
            $map_next['id']  = array('gt', I('get.id'));
            $data_next = $news->where($map_next)->order('id asc')->limit(1)->find();
            if(empty($data_next)){
                $this->assign('next', '已是最后一条');
            }else{
                $str_len= mb_strlen($data_next['title'],'utf-8');
                if($str_len>15){
                    $data_next['title'] = mb_substr($data_next['title'],0,15,'utf-8').'...';
                }
                $this->assign('next', $data_next['title']);
            }

        }

      //点击跳转到上一条
      if(I('get.action')=='pre'){

        $map['ntype']= '3';
        $map['id']  = array('lt', I('get.id'));
        $data = $news->where($map)->order('id desc')->limit(1)->select();

        if(count($data)>0){
           $newOrderid = $data[0]['id'];
            //跳转到上一条表页
            $this->redirect('law_detail'."/id/".$newOrderid);
        }
      }

        //点击跳转到下一条
        if(I('get.action')=='next'){

            $map['ntype']= '3';
            $map['id']  = array('gt', I('get.id'));
            $data = $news->where($map)->order('id asc')->limit(1)->select();

            if(count($data)>0){
                $newOrderid = $data[0]['id'];
                //跳转到上一条表页
                $this->redirect('law_detail'."/id/".$newOrderid);
            }
        }

        foreach ($new as $key=>$value) {
            $str_len= mb_strlen($new[$key]['title'],'utf-8');
           if($str_len>15){
             $new[$key]['title'] = mb_substr($new[$key]['title'],0,15,'utf-8').'...';
           }
        }

        $name= __FUNCTION__; $this->assign('name', 'law');
        $this->assign('new', $new);
        $this->assign('title', $title);
        $this->assign('cate_title', $cate_title);
        $this->display();
    }

    //搜索功能
    public function search(){

        $search= M('News');
        $key= trim(I('get.key'));
        $list='';
        if(empty($key)){
            $this->assign('list', $list);
            $this->display();
            exit();
        }

        $where['title'] = array('like',"%{$key}%");

        $count = $search->where($where)->count();
        $pagecount = 5;
        $app= __APP__.'/index.php/search/';
        $list=$this->getPage($count, $pagecount, $search, $where, $app);

        foreach ($list as $key2=>$value){

            //标题字符串截取
            $str_len= mb_strlen($list[$key2]['title'],'utf-8');
            if($str_len>35){

                $list[$key2]['title'] = mb_substr($value['title'],0,35,'utf-8').'...';
            }

            $list[$key2]['title']= preg_replace("/($key)/i","<b style=\"color:red\">\\1</b>", $list[$key2]['title']);
        }

        $this->assign('key', $key);
        $this->assign('list', $list);
        $this->display();
    }



    //调用验证码
    public function verify(){

        $Verify = new \Think\Verify();
        $Verify->imageH = 40;
        $Verify->imageW = 120;
        $Verify->length = 4;
        $Verify->fontSize = 18;
        $Verify->useNoise = false;

        $Verify->entry();
    }

    //文件上传
     public function upload(){

          $user=D('Users');

          $id= session('id');

          $config = array( 'maxSize' =>3145728, 'autoSub'=>false, 'saveExt'=>'jpg', 'exts'=>array('jpg', 'gif', 'png', 'jpeg'),
                           'rootPath'=>true, 'replace'=>true,'saveName'=>$id, 'rootPath'=> './Public/Uploads/head/');

          $upload = new \Think\Upload($config);  // 实例化上传类

          $info = $upload->upload();

           if(!$info) {

               $this->error($upload->getError());

           }else{
               // 生成缩略图
               $data['head_img']= './Public/Uploads/head/'.$id.'.jpg';
               $user->where('id=' .$id)->save($data);

               $image = new \Think\Image();
               $image->open('./Public/Uploads/head/'.$id.'.jpg');
               $image->thumb(90, 110,\Think\Image::IMAGE_THUMB_CENTER)->save('./Public/Uploads/head/'.$id.'.jpg');
               // 上传成功

              }

       }

       //分页
       public function getPage($count, $pagecount, $db, $where, $app){

           $page = new \Think\Page($count , $pagecount);
           $page->setConfig('first','首页');
           $page->setConfig('prev','上一页');
           $page->setConfig('next','下一页');
           $page->setConfig('last','尾页');
           $page->setConfig('theme',
               '
                       %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 共 %TOTAL_PAGE% 页,
                       <form action='.$app.' method="get">
                        到第 <input name="p" type="text" >页<input class="submit" type="submit" value="确定">
                                     ');
           $show = $page->show();
           $list = $db->where($where)->limit($page->firstRow.','.$page->listRows)->select();
           $this->assign('list',$list);
           $this->assign('page',$show);
           return $list;
       }

    //下载类
    public function download ($filename, $show_name='',$content='',$expire=180) {
        if(file_exists($filename)){
            $length = filesize($filename);
        }elseif(is_file(UPLOAD_PATH.$filename)){
            $filename = UPLOAD_PATH.$filename;
            $length = filesize($filename);
        }elseif($content != ''){
            $length = strlen($content);
        }else {
            throw_exception($filename.L('下载文件不存在！'));
        }
        if(empty($show_name)){
            $show_name = $filename;
        }
        $show_name = basename($show_name);
        if(empty($filename)){
            $type = mime_content_type($filename);
        }else{
            $type = "application/octet-stream";
        }
        //发送Http Header信息 开始下载
        header("content-type:text/html; charset=utf-8");
        header("Pragma: public");
        header("Cache-control: max-age=".$expire);
        //header('Cache-Control: no-store, no-cache, must-revalidate');
        header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
        //下面一行就是改动的地方，即用iconv("UTF-8","GB2312//TRANSLIT",$showname)系统函数转换编码为gb2312
        header("Content-Disposition: attachment; filename=". iconv("UTF-8","GB2312",$show_name)); header("Content-Length: ".$length);
        header("Content-type: ".$type);
        header('Content-Encoding: none');
        header("Content-Transfer-Encoding: binary" );
        if($content == '' ) {
            readfile($filename);
        }
        else { echo($content); }
        exit();
    }



        //微信首页
        public function wxindex(){
            $this->keyword = $_POST['keyword'];
            $Positions = M('Positions');
            $where='1 ';
            if($this->keyword){
                $where.="and (`company` LIKE '%".$this->keyword."%' or `name` LIKE '%".$this->keyword."%')";
            }
            $list = $Positions->where($where)->select();
            $this->assign('list',$list);
            $this->display();
        }

    //微信关于我们
    public function wxabout(){

        $news= D("News");
        $list= $news->where("ntype=1")->select();
        $count= $news->where("ntype=1")->field('id')->select();
        if(I('get.id')=='' ||   !is_numeric(I('get.id')) ){
            $about= $news->where("id=" .$list[0][id])->find();
            $about['content']=strip_tags($about['content']);
            $this->assign('id', $list[0][id]);

        }else{
            $about= $news->where("id=" .I('get.id'))->find();
            $about['content']=strip_tags($about['content']);
        }


        foreach($list as $key => $val){

            if(I('get.id') == $val["id"]){
                $this->assign('id', $val["id"]);
            }else{

            }

        }

        $name= __FUNCTION__; $this->assign('name', $name);
        $this->assign('list', $list);
        $this->assign('about', $about);
        $this->display();
    }

    //微信职位详情
    public function wxposition(){
        $Positions = M('Positions');
        $pos_id = I('get.pos_id');
        $where = '1 ';
        if($pos_id){
            $where .= 'and pos_id='.$pos_id;
        }
        $data = $Positions->where($where)->find();
//        var_dump($data);exit;
        $this->assign('data',$data);
        $this->display();
    }

    //微信职位申请
    public function wxposition_apply(){
        if($_SESSION['islogin']== '1'){
            $apply_record["user_id"]=session('id');
            $apply_record["pos_id"] = I("get.pos_id");
            $apply_record["apply_time"] = time();
            $Model=M('Position_apply_recorder');
            if(!$Model->where("user_id=".$apply_record["user_id"]." and pos_id=".$apply_record["pos_id"])->getField("record_id")){
                $Model->add($apply_record);
                $this->success("申请成功", __APP__ . "/wxindex");
            }else{
                $this->error("不能重复申请!", __APP__ . "/wxposition?pos_id=".$apply_record["pos_id"]);
            }

        }else{
            $this->error("请登入!", __APP__ . "/wxlogin");
        }
    }

    //微信留言
    public function wxmessage(){
        $Contact = M('Contact');
        if($_POST){
            if($Contact->add($_POST)){
                echo "<script language=\"javascript\">alert('留言成功！');window.location.href='/wxindex';</script>";
            };
        }
        $this->display();
    }


}